package com.yvan.leto.server.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.yvan.YvanUtil;
import com.yvan.leto.SegmentHelper;
import com.yvan.leto.server.dao.RepositoryPersistent;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Component(ConfigParserService.PARSER_PREFIX + SegmentHelper.YAML)
public class YamlParser implements IParser {

    @Autowired
    private RepositoryPersistent repositoryPersistent;

    @Override
    public String process(ParserContext context) {
        //合并所有 Resource
        List<File> resourceList = Lists.newArrayList();
        for (val relativePath : context.getSegment().getConfigFiles()) {
            resourceList.addAll(repositoryPersistent.repositoryEnvFileList(context.getEnv(), relativePath));
        }

        JsonNode jsonNode = IParser.mergeResourceContentToJsonNode(resourceList);

        //将 Yaml 格式转换为 Properties 格式
        return YvanUtil.toYaml(jsonNode);
    }
}
